    /*
    Title: 					IV functions
    Project: 				Medellin Gang Governance
    Authors: 				Zach
    Date Created: 			Sept 27, 2019
    Description: 			This .do file provides a function used in the analysis code
    */
    ********************************************************************************
    * Define the analysis function ************************************************
    ********************************************************************************

    cap program drop label_itt_regression
    program define label_itt_regression
        set matsize 10000
        syntax  [if/], outcomevars(namelist) FILENAME(name) [fixedeffects(varlist) controls(varlist) terms(namelist) mainhead(string)]

        cap mat drop results
        cap mat drop stars

        if "`fixedeffects'"!="" local vfe abs(`fixedeffects')
        if "`fixedeffects'"=="" local vfe noa

        if regexm("`terms'","ny")==1 local yterms tyav
        if regexm("`terms'","ny")==1 local var_suffix tyav

        if regexm("`terms'","lt")==1 local lterms ltav
        if regexm("`terms'","lt")==1 local var_suffix ltav


    local table_rownames

    foreach raw_var_name in `outcomevars' {
        local table_rownames `table_rownames' `raw_var_name'_`var_suffix'
    }



        local ifa if `if'
        local rownum = `: word count `outcomevars''
        local colnum =  11 * `: word count `terms''

        mat define results = J(`rownum',`colnum', .)
        mat define stars   = J(`rownum',`colnum', 0)


        local i = 0
        local loutcomevars
        /* Set restrictions for short and long term surveys */
        foreach y in `outcomevars' {
            local ++i

            if `i' == 1 local loutcomevars `"`loutcomevars' "${lab`y'f}" "'
            if `i' >  1 local loutcomevars `"`loutcomevars' \ "${lab`y'f}" "'
            /* Loop over short term and long term */
            local t = 0
            foreach term in `lterms' `yterms' {
                /* Get treatment means mean */
                qui sum `y'_`term'  if `if' & control == 1
                    mat results[`i',2 + `t'] = r(mean)
                if `r(N)' {
                        /* Run regression */
                        qui reghdfe `y'_`term' tpassonly cashassonly tpcashass `controls' `ifa', vce(r) `vfe'
                            mat results[`i',1 + `t'] = e(N)

                            /* Recover Coefficients, standard errors, and p-values */

                            mat results[`i',3 + `t'] = _b[tpassonly]
                            mat results[`i',4 + `t'] = _se[tpassonly]
                            mat results[`i',5 + `t'] = 2*ttail(e(df_r),abs(_b[tpassonly]/_se[tpassonly]))

                            mat results[`i',6 + `t']= _b[cashassonly]
                            mat results[`i',7 + `t']= _se[cashassonly]
                            mat results[`i',8 + `t']= 2*ttail(e(df_r),abs(_b[cashassonly]/_se[cashassonly]))

                            mat results[`i',9 + `t']= _b[tpcashass]
                            mat results[`i',10+ `t']= _se[tpcashass]
                            mat results[`i',11+ `t']= 2*ttail(e(df_r),abs(_b[tpcashass]/_se[tpcashass]))

                local colcount = 0
                foreach tcount in tpassonly cashassonly tpcashass{
                   mat stars[`i',3 + `colcount'+ `t'] = cond(results[`i',5 + `colcount' + `t'] <= .01,3, ///
                                                        cond(results[`i',5 + `colcount' + `t'] <= .05,2, ///
                                                        cond(results[`i',5 + `colcount' + `t'] <= .1,1,0  ///
                   )))
                   local colcount = `colcount' + 3
                }
                }
                local t = `t' + 10
                }
                }

    if regexm("`terms'","lt")==1 {
        local multicollt 1,2,11
        local tmulticollt 2,4,3;2,7,3;2,10,3
        local labellt `" "1 Year Endline", "", "", "", "", "", "", "", "", "","" "'
        local tlabellt `" "N", "\thead{Control \\ Mean}","\uline{\hfill Therapy Only \hfill}","","","\uline{\hfill Cash Only \hfill}","","","\uline{\hfill Both \hfill}","","" "'
        local clabellt `" "", "","\thead{\$\beta$}","SE","p-values","\thead{\$\beta$}","SE","p-values","\thead{\$\beta$}","SE","p-values" "'
        local nlabellt `" "(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)","(9)","(10)","(11)" "'
        local ndecimallt  0,3,3,3,3,3,3,3,3,3,3
        }

    if regexm("`terms'","ny")==1 {
        local multicol9y 1,13,11
        local tmulticol9y 2,15,3;2,18,3;2,21,3
        local label9y  `" "\uline{\hfill Average Treatment Effects \hfill}", "", "", "", "", "", "", "", "", "", " " "'
        local tlabel9y `" "N" ,"\thead{Control \\ Mean}","\uline{\hfill Therapy Only \hfill}","","","\uline{\hfill Cash Only \hfill}","","","\uline{\hfill Both \hfill}","","" "'
        local clabel9y `" "","","\thead{\$\beta$}","SE","p-values","\thead{\$\beta$}","SE","p-values","\thead{\$\beta$}","SE","p-values" "'
        local nlabel9y `" "(11)","(12)","(13)","(14)","(15)","(16)","(17)","(18)","(19)","(20)","(22)" "'
        local ndecimal9y  0,3,3,3,3,3,3,3,3,3,3
        if regexm("`terms'","lt")==0 local nlabel9y `""(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)","(9)","(10)","(11)" "'
        if regexm("`terms'","lt")==0 local multicol9y 1,2,11
        if regexm("`terms'","lt")==0 local tmulticol9y 2,4,3;2,7,3;2,10,3
        }

    if regexm("`terms'","lt")==1 & regexm("`terms'","ny")==1 local comma ,
    if regexm("`terms'","lt")==1 & regexm("`terms'","ny")==1 local semicolon ;




*    matlist results
*    di "`table_rownames'"

    mat rownames results = `table_rownames'
    matlist results

    fsum `table_rownames'


    if "`mainhead'"!="" local mainhead \multirow{3}{*}{`mainhead'}



         qui frmttable, statmat(results)  varlabels sdec(`ndecimallt'`comma'`ndecimal9y')

         qui frmttable, statmat(results) varlabels sdec(`ndecimallt'`comma'`ndecimal9y')  annotate(stars) asymbol(*,**,***)

    frmttable using outfiles/tables/`filename', ///
    ctitle("",  `label9y' `comma' `labellt'  \ "`mainhead'" , `tlabel9y' `comma' `tlabellt' \ " ",`clabel9y' `comma' `clabellt' \  "",`nlabel9y' `comma' `nlabellt' ) ///
    tex fragment replace nocenter squarebrack  varlabels ///
    multicol(`multicollt'`semicolon'`multicol9y';`tmulticollt'`semicolon'`tmulticol9y')




        end
